package com.example.java_gobang.game;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.concurrent.ConcurrentHashMap;

@Component
public class OnlineUserManager {
    //这个哈希表表示当前用户在游戏大厅的在线状态
    private ConcurrentHashMap<Integer, WebSocketSession> gameHall = new ConcurrentHashMap<>();

    //这个哈希表就用来表示当前用户在游戏房间的在线状态
    private ConcurrentHashMap<Integer, WebSocketSession> gameRoom = new ConcurrentHashMap<>();

    public void enterGameHall(int userId,WebSocketSession webSocketSession){
        gameHall.put(userId, webSocketSession);   //进入游戏大厅
    }

    public void exitGameHall(int userId){
        gameHall.remove(userId);  //退出游戏大厅
    }

    public WebSocketSession getFromGameHall(int userId){
        return gameHall.get(userId);   //查找
    }


    public void enterGameRoom(int userId,WebSocketSession webSocketSession){
        gameRoom.put(userId, webSocketSession);  //进入房间
    }

    public void exitGameRoom(int userId){
        gameRoom.remove(userId);  //退出房间
    }

    public WebSocketSession getFromGameRoom(int userId){
        return gameRoom.get(userId);   //根据玩家ID查找
    }
}
